<?php
require_once 'Sabai/Cloud.php';

class Sabai_Template_Default_Helper_XiggTagCloud extends Sabai_Template_Default_Helper
{
    function execute($cache = true, $limit = 0, $script = SABAI_URL_SCRIPT)
    {
        $id = 'XiggTagCloud' . $limit;
        if ($cache) {
            $cacher =& $this->_locator->getService('Cacher');
            if ($data = $cacher->get($id, 'XiggTagCloud')) {
                $data = unserialize($data);
            } else {
                $data = $this->_getCloudData($limit, $script);
                $cacher->save(serialize($data), $id, 'XiggTagCloud');
            }
        } else {
            $data = $this->_getCloudData($limit, $script);
        }
        return $data;
    }

    function _getCloudData($limit, $script)
    {
        $model =& $this->_locator->getService('Model');
        $tag_gw =& $model->getGateway('Tag');
        $tags = $tag_gw->getTagsWithNodeCount($limit, 'tag_name');
        ksort($tags);
        $cloud =& new Sabai_Cloud();
        foreach (array_keys($tags) as $i) {
            $cloud->addElement($tags[$i]['tag_name'],
                               Sabai_URL::get('/tag/' . rawurlencode($tags[$i]['tag_name']), array(), '', $script),
                               $tags[$i]['node_count']);
        }
        return $cloud->build();
    }
}